package xm;

import chapter5.entity.Admin;
import chapter5.service.AdminService;
import chapter5.service.impl.AdminServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/add.do")
public class xzServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理Get请求
        //处理请求的代码
        request.setCharacterEncoding("utf-8");
        //设置相应的格式
        response.setContentType("text/html;charset=utf-8");
        //设置响应的编码格式
        response.setCharacterEncoding("utf-8");
        PrintWriter out=response.getWriter();

        String jsonName=request.getParameter("obj");
        System.out.println(jsonName);
        Admin admin= JSON.parseObject(jsonName,Admin.class);
        String name=admin.getName();
        String pwd=admin.getPwd();
        Admin admin1=new Admin(name,pwd);
        AdminServiceImpl impl=new AdminServiceImpl();
        int count=impl.adminService(admin1);
        String shuchu=null;
        if (count>0){
            List<Admin> list=impl.selct();
            for (int i=0;i<list.size();i++){
                if (i==list.size()-1){
                    shuchu=list.get(i).toString();
                    out.println(shuchu);
                }
            }
        }
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
